/*
 * Copyright 2020-2030 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.wwl.deque;


@SuppressWarnings("all")
public class Deque<T> {

    private T data[];
    private int front, tail;
    private int size;

    public Deque(int capacity) {
        data = (T[]) new Object[capacity];
        front = 0;
        tail = 0;
        size = 0;
    }

    public Deque() {
        this(10);
    }

    public int capacity() {
        return data.length;
    }

    public boolean isEmpty() {
        return size == 0;
    }

    public int size() {
        return size;
    }

    public void addLast(T t) {
        add();
        data[tail] = t;
        tail = (tail + 1) % data.length;
        size++;
    }

    private void add() {
        if (size == capacity()) {
            resize(capacity() * 2);
        }
    }

    public void addFront(T t) {
        add();
        data[front = front == 0 ? data.length - 1 : front - 1] = t;
        size++;
    }

    public T removeFirst() {
        throwException("Cannot dequeue from an empty queue.");
        T t = data[front];
        data[front] = null;
        front = (front + 1) % data.length;
        size--;
        cut();
        return t;
    }

    private void cut() {
        if (size() == capacity() / 4 && capacity() / 2 != 0) {
            resize(capacity() / 2);
        }
    }

    private void throwException(String msgs) {
        if (isEmpty()) {
            throw new IllegalArgumentException(msgs);
        }
    }

    public T removeLast() {
        throwException("Cannot dequeue from an empty queue.");
        T t = data[tail = tail == 0 ? data.length - 1 : tail - 1];
        data[tail] = null;
        size--;
        cut();
        return t;
    }

    public T getLast() {
        throwException("Queue is empty.");
        return data[tail == 0 ? data.length - 1 : tail - 1];
    }

    public T getFront() {
        throwException("Queue is empty.");
        return data[front];
    }

    private void resize(int capacity) {
        T[] newData = (T[]) new Object[capacity];
        for (int i = 0; i < size; i++) {
            newData[i] = data[(i + front) % data.length];
        }
        data = newData;
        front = 0;
        tail = size;
    }

    @Override
    public String toString() {
        return "Deque front[" +
                "data=" + generate(data) +
                "] tail";
    }

    private String generate(T[] data) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < size(); i++) {
            sb.append(data[(i + front) % data.length]).append(i != size - 1 ? ", " : "");
        }
        return sb.toString();
    }
}
